﻿/*

filipesilvestrim - An open-source ActionScript 3.0 repository of codes for Game Developers 

http://code.google.com/p/filipesilvestrim/

Copyright (c) 2009 Filipe Ghesla Silvestrim, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

package com.filipesilvestrim.game.level
{	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.utils.Dictionary;
	
	import com.filipesilvestrim.manager.AbstractStateChange;
	import com.filipesilvestrim.game.core.Game;
	
	public class LevelManager extends AbstractStateChange
	{
		// ___________________________________________________________________ CONSTANTS
		static public const		NOW			: String	= "levelManager_now";
		static public const		ON_BUILD	: String	= "levelManager_build";
		static public const		ON_DESTROY	: String	= "levelManager_destroy";
		
		// ___________________________________________________________________ CLASS PROPERTIES
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		private var _changeOnBuild		: Dictionary;
		private var _changeOnDestroy	: Dictionary;
		private var _viewContainer		: DisplayObjectContainer;
		private var _gameRef			: Game;
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		public function get game	() 			: Game 						{ return _gameRef; }
		public function get viewContainer	() 	: DisplayObjectContainer 	{ return _viewContainer; }
		
		// ___________________________________________________________________ CONSTRUCTOR
		
		public function LevelManager (gameRef : Game, viewContainer : DisplayObjectContainer ) 
		{
			_gameRef			= gameRef;
			_viewContainer		= viewContainer;
			_changeOnBuild		= new Dictionary(true);
			_changeOnDestroy	= new Dictionary(true);
		}
		
		// ___________________________________________________________________ PUBLIC METHODS
		/**
		 * Will replicate all changes in the object to all levels
		 */
		public function addToAll (identifier : String, namePropertie : String, value : *, when : String) : void
		{
			switch (when)
			{
				case NOW:
					internalChange(namePropertie, value);
					break;
				case ON_BUILD:
					_changeOnBuild[identifier] = [namePropertie, value];
					break;
				case ON_DESTROY:
					_changeOnBuild[identifier] = [namePropertie, value];
					break;
			}
		}
		// ___________________________________________________________________ PRIVATE METHODS
		override protected function internalBuild () : void
		{
			for each (var objChange : Array in _changeOnBuild)
				internalChange(objChange[0], objChange[1]);
			
			try
			{	
				_next.build();
				_viewContainer.addChild((_next as DisplayObject));
			}
			catch ( e : Error) { trace (e.message); }
		}
		
		override protected function internalDestroy () : void
		{
			for each (var objChange : Array in _changeOnBuild)
				internalChange(objChange[0], objChange[1]);
				
			try
			{
				_current.destroy();
				_viewContainer.removeChild((_previous as DisplayObject));
			}
			catch ( e : Error) { trace (e.message); }
		}
		
		private function internalChangeObj ( obj : Object ) : void
		{
			for each (var level : AbstractLevel in data)
			{
				for (var propertieName : String in obj)
				{
					level.changePropertie(propertieName, obj[propertieName]);
				}
			}
		}
		
		private function internalChange ( propPath : String, value : * ) : void
		{
			for each (var level : AbstractLevel in data)
			{
				level.addPropertie(propPath, value);
			}
		}
		
		// ___________________________________________________________________ EVENTS
	}
}

